package com.kly.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.google.common.eventbus.AsyncEventBus;
import com.kly.enums.ParameterTypeEnum;
import com.kly.user.enums.AppEnum;
import com.kly.user.eventbus.ActionEnum;
import com.kly.user.eventbus.Event;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class PushTestController {

    @Resource
    AsyncEventBus pushEventBus;

    @RequestMapping("/pushEvent")
    public void pushMach(String userCode,String toUserCode,int appType,int type){

        AppEnum appEnum = AppEnum.fromType(String.valueOf(appType));
        pushEventBus.post(buildEvent(userCode, toUserCode, appEnum,type));
    }


    private Event buildEvent(String fromUseCode, String toUserCode, AppEnum appEnum,int type) {
        Map<String, Integer> params = Maps.newHashMap();
        params.put("type", type);
        return Event.builder().eventActionEnum(ActionEnum.PUSH_MSG).fromUserCode(fromUseCode).toUserCode(toUserCode)
                .app(appEnum).content(JSONObject.toJSONString(params)).build();
    }
}
